/************************************
*  Copyright (C) 2010 Julien MOTTIN.
*  Contact: julien.mottin@gueydan.eu
*
*  This file is part of Gueydan.
*
*  Gueydan is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Gueydan is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with Gueydan.  If not, see <http://www.gnu.org/licenses/>.
*
*************************************/
// Local
#include "color.h"
#include "util.h"

// QtCore
#include <QMap>
#include <QString>

Color::Color(const double iColor, const ColorUnit iUnit)
{
    _Value = iColor;
    switch (iUnit)
    {
    case SRM:
        break;
    case EBC:
        _Value*=CCoeffEBC;
        break;
    case Lovibond:
        _Value = _Value*CCoefLovibond + CShiftLovibond;
        break;
    }
}

Color::Color(const Color &iC)
{
    _Value = iC._Value;
}

Color & Color::operator=(const Color &iC)
{
    if (this!=&iC)
    {
        _Value = iC._Value;
    }
    return *this;
}

Color::~Color()
{
}

bool Color::operator==(const Color &iC) const
{
    return (_Value==iC._Value);
}

bool Color::operator!=(const Color &iC) const
{
    return (_Value!=iC._Value);
}

bool Color::operator<(const Color &iC) const
{
    return (_Value<iC._Value);
}

bool Color::operator<=(const Color &iC) const
{
    return (_Value<=iC._Value);
}

bool Color::operator>(const Color &iC) const
{
    return (_Value>iC._Value);
}

bool Color::operator>=(const Color &iC) const
{
    return (_Value>=iC._Value);
}

double Color::Value(const ColorUnit iUnit) const
{
    double oVal = _Value;
    switch (iUnit)
    {
    case SRM:
        break;
    case EBC:
        oVal/=CCoeffEBC;
        break;
    case Lovibond:
        oVal = (_Value-CShiftLovibond)/CCoefLovibond;
        break;
    }

    return oVal;
}

// Global Color QMap
static QMap<QString,ColorUnit> ColorUnitMap;
static bool ColorUnitMapInit = false;

void Color::FromString(const QString &iString, bool *OK)
{
    bool tmpOK = false;

    if (!ColorUnitMapInit)
    {
        ColorUnitMap["srm"] = SRM;
        ColorUnitMap["ebc"]  = EBC;
        ColorUnitMap["l"] = Lovibond;
        ColorUnitMap["°l"] = Lovibond;
        ColorUnitMapInit = true;
    }

    QString Unit;
    double Val;
    StringToUnit(iString,Val,Unit);

    // Case no Unit
    if (Unit.isEmpty())
    {
        // Just copy value
        _Value = Val;
        tmpOK = true;
    }
    else
    {
        if (ColorUnitMap.contains(Unit))
        {
            // Build Color with proper unit
            ColorUnit zeUnit = ColorUnitMap[Unit];
            (*this)=Color(Val,zeUnit);
            tmpOK = true;
        }
    }

    if (0!=OK)
        (*OK)=tmpOK;
}
